package org.glut.wechat.moments.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.glut.wechat.moments.domain.Moments;
import org.glut.wechat.moments.domain.MomentsComment;
import org.glut.wechat.moments.domain.MomentsNotice;
import org.glut.wechat.moments.mapper.MomentsCommentMapper;
import org.glut.wechat.moments.mapper.MomentsMapper;
import org.glut.wechat.moments.mapper.MomentsNoticeMapper;
import org.glut.wechat.moments.service.MomentsCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 朋友圈评论服务实现类
 */
@Service
public class MomentsCommentServiceImpl extends ServiceImpl<MomentsCommentMapper, MomentsComment> implements MomentsCommentService {


    @Autowired
    private MomentsNoticeMapper momentsNoticeMapper;
    @Autowired
    private MomentsMapper momentsMapper;

    @Transactional
    @Override
    public void publishComment(MomentsComment comment) {
        comment.setCreateTime(LocalDateTime.now());
        baseMapper.insert(comment);
        Moments moments = momentsMapper.selectById(comment.getMomentId());
        if(!moments.getUserId().equals(comment.getUserId())){
            MomentsNotice momentsNotice = new MomentsNotice();
            momentsNotice.setCreateTime(LocalDateTime.now());
            momentsNotice.setMomentId(comment.getMomentId());
            momentsNotice.setOperationUserId(comment.getUserId());
            momentsNotice.setMomentId(moments.getId());
            momentsNotice.setOperationUserId(comment.getUserId());
            momentsNotice.setNoticeUserId(moments.getUserId());
            momentsNotice.setType(1);
            momentsNotice.setCommentId(comment.getId());
            if(comment.getPid() != 0) {
                momentsNotice.setIsReply(1);
            }else {
                momentsNotice.setIsReply(0);
            }
            momentsNoticeMapper.insert(momentsNotice);
        }
        if(comment.getPid() != 0){
            Integer userId = baseMapper.selectById(comment.getPid()).getUserId();
            if(!userId.equals(comment.getUserId()) && !userId.equals(moments.getUserId())){
                MomentsNotice momentsNotice = new MomentsNotice();
                momentsNotice.setCreateTime(LocalDateTime.now());
                momentsNotice.setMomentId(comment.getMomentId());
                momentsNotice.setOperationUserId(comment.getUserId());
                momentsNotice.setMomentId(moments.getId());
                momentsNotice.setCommentId(comment.getId());
                momentsNotice.setOperationUserId(comment.getUserId());
                momentsNotice.setNoticeUserId(userId);
                momentsNotice.setType(1);
                momentsNotice.setIsReply(1);
                momentsNoticeMapper.insert(momentsNotice);
            }
        }

    }

    @Override
    public void deleteComment(Integer id) {
        QueryWrapper<MomentsComment> deleteWrapper = new QueryWrapper<>();
        deleteWrapper.eq("id", id).or().eq("pid", id);
        baseMapper.delete(deleteWrapper);
    }
}
